package db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class MyDatabaseHelper extends SQLiteOpenHelper {

    /// 鲜花表
    public static final String CREATE_FLOWER = "create table Flowers ("
            + "id integer primary key autoincrement, "
            + "name text, "/// 鲜花名称
            + "category integer, "/// 鲜花类别
            + "origin text, "/// 鲜花产地
            + "url text," /// 鲜花图片
            + "stock integer," /// 库存
            + "describe text," /// 描述
            + "createAt varchar(60)," /// 添加时间
            + "price real)"; /// 鲜花价格

    /// 鲜花销售记录表
    public static final String CREATE_SALES_RECORD = "create table SalesRecord ("
            + "id integer primary key autoincrement, "
            + "date Date," /// 时间
            + "flower integer," /// 鲜花Id
            + "count int," /// 数量
            + "total real, "  /// 总价
            + "client varchar(60), "  /// 客户名称
            + "remark varchar(200),"   /// 备注
            + "emp varchar(60),"   /// 员工
            + "createAt Date,"    /// 创建日期
            + "updateAt Date)";

    /// 鲜花类别表
    public static final String CREATE_FLOWER_TYPE = "create table Category ("
            + "id integer primary key autoincrement, "
            + "name text)";

    /// 鲜花图片
    public static final String CREATE_PHOTO= "create table Photo ("
            + "id integer primary key autoincrement, "
            + "flowerId integer," /// 鲜花id
            + "url text)";
    private final Context mContext;

    public MyDatabaseHelper(Context context, String name,
                            SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_FLOWER);
        db.execSQL(CREATE_SALES_RECORD);
        db.execSQL(CREATE_FLOWER_TYPE);
        db.execSQL(CREATE_PHOTO);
        Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists Flowers");
        db.execSQL("drop table if exists Category");
        db.execSQL("drop table if exists FlowerType");
        db.execSQL("drop table if exists Photo");
        onCreate(db);
    }

}